﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using HCFramework.API.LINQ;

namespace HCFramework.Product.ProductManage
{
    public partial class ProductImageManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindHeader();
            }
        }

        private void BindHeader()
        {
            HCFramework.API.LINQ.Product product = C.ProductController.GetByID(M.UrlQuery.productID.Value);
            litCatName.Text = C.ProductCategoryController.GetPathName(product.CategoryID);
            litProductName.Text = product.ProductName;
        }

        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.ProductImages
                         where a.ProductId == M.UrlQuery.productID.Value
                         select a;

            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                TextBox TextBox1 = e.Item.FindControl("TextBox1") as TextBox;
                FileUpload FileUpload1 = e.Item.FindControl("FileUpload1") as FileUpload;
                ProductImage pi = new ProductImage();
                pi.IsMainImage = false;
                pi.ProductId = M.UrlQuery.productID.Value;
                pi.URL = TextBox1.Text;
                if (FileUpload1.HasFile) pi.URL = M.FileM.UploadAndReturnURL(FileUpload1, "/Product/Image/");
                C.ProductImageController.Insert(pi);
            }
            if (e.CommandName == "ChangeMainImage")
            {
                int id = int.Parse(e.CommandArgument + "");
                HCDataContext db = new HCDataContext();
                ProductImage thisPI = C.ProductImageController.GetByID(id, db);
                foreach (ProductImage pi in thisPI.Product.ProductImages)
                {
                    pi.IsMainImage = false;
                    C.ProductImageController.Update(pi, db);
                }
                thisPI.IsMainImage = true;
                C.ProductImageController.Update(thisPI, db);
            }
            if (e.CommandName == "Xoa")
            {
                int id = int.Parse(e.CommandArgument + "");
                C.ProductImageController.Delete(id);
            }
            LoadGridData();
        }
        protected void RadGridMain_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                ImageButton ImageButton1 = e.Item.FindControl("ImageButton1") as ImageButton;
                ImageButton1.ImageUrl = "/Image/" + ImageButton1.ImageUrl + ".jpeg";
            }
        }

        protected void linkBack_Click(object sender, EventArgs e)
        {
            M.Common.GoToPreviousPage();
        }
    }
}